package com.ruoyi.project.modules.errorCode.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.project.modules.errorCode.domain.ErrorCode;
import com.ruoyi.project.modules.errorCode.mapper.ErrorCodeMapper;
import com.ruoyi.project.modules.errorCode.service.IErrorCodeService;
import com.ruoyi.project.modules.errorCodeCategory.domain.ErrorCodeCategory;
import com.ruoyi.project.modules.errorCodeCategory.mapper.ErrorCodeCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 错误码Service业务层处理
 *
 * @author smallrain
 * @date 2022-04-04
 */
@Service
public class ErrorCodeServiceImpl implements IErrorCodeService {
    @Resource
    private ErrorCodeMapper errorCodeMapper;

    /**
     * 查询错误码
     *
     * @param errorCodeId 错误码主键
     * @return 错误码
     */
    @Override
    public ErrorCode selectErrorCodeByErrorCodeId(Long errorCodeId) {
        return errorCodeMapper.selectErrorCodeByErrorCodeId(errorCodeId);
    }

    /**
     * 查询错误码列表
     *
     * @param errorCode 错误码
     * @return 错误码
     */
    @Override
    public List<ErrorCode> selectErrorCodeList(ErrorCode errorCode) {
        return errorCodeMapper.selectErrorCodeList(errorCode);
    }

    @Resource
    private ErrorCodeCategoryMapper errorCodeCategoryMapper;

    /**
     * 新增错误码
     *
     * @param errorCode 错误码
     * @return 结果
     */
    @Override
    public synchronized int insertErrorCode(ErrorCode errorCode) {
///        ErrorCodeCategory errorCodeCategory = errorCodeCategoryMapper.selectErrorCodeCategoryByErrorCodeCategoryId(errorCode.getErrorCodeCategoryId());
///        String errorCode1 = errorCodeCategory.getErrorCodeCategoryCode() + errorCode.getErrorCode();
///        ErrorCode code = new ErrorCode();
///        code.setErrorCode(errorCode1);
///        List<ErrorCode> errorCodes = this.errorCodeMapper.selectErrorCodeList(code);
///        if (CollectionUtil.isNotEmpty(errorCodes)) {
///            throw new BusinessException("错误码重复");
///        }
///        errorCode.setErrorCode(errorCode1);
        ErrorCodeCategory errorCodeCategory = errorCodeCategoryMapper.selectErrorCodeCategoryByErrorCodeCategoryId(errorCode.getErrorCodeCategoryId());
        int result = errorCodeMapper.insertErrorCode(errorCode);
        if (result <= 0) {
            return result;
        }
        // 生成错误码并更新
        String code = String.format("%s%05d", errorCodeCategory.getErrorCodeCategoryCode(), errorCode.getErrorCodeId());
        ErrorCode uptBean = new ErrorCode();
        uptBean.setErrorCodeId(errorCode.getErrorCodeId());
        uptBean.setErrorCode(code);
        return errorCodeMapper.updateErrorCode(uptBean);
    }

    /**
     * 修改错误码
     *
     * @param errorCode 错误码
     * @return 结果
     */
    @Override
    public int updateErrorCode(ErrorCode errorCode) {
        return errorCodeMapper.updateErrorCode(errorCode);
    }

    /**
     * 批量删除错误码
     *
     * @param errorCodeIds 需要删除的错误码主键
     * @return 结果
     */
    @Override
    public int deleteErrorCodeByErrorCodeIds(String errorCodeIds) {
        return errorCodeMapper.deleteErrorCodeByErrorCodeIds(Convert.toStrArray(errorCodeIds));
    }

    /**
     * 删除错误码信息
     *
     * @param errorCodeId 错误码主键
     * @return 结果
     */
    @Override
    public int deleteErrorCodeByErrorCodeId(Long errorCodeId) {
        return errorCodeMapper.deleteErrorCodeByErrorCodeId(errorCodeId);
    }
}
